# libc/r3ka Makefile

VPATH  = @srcdir@
srcdir = @srcdir@
top_builddir = @top_builddir@
abs_top_builddir = @abs_top_builddir@

LIB_TYPE     = @LIB_TYPE@

EXTRA_CFLAGS = -mr3ka --max-allocs-per-node 25000
SAS = $(top_builddir)/bin/sdasrab

# override PORTDIR defined by super (parent) makefile
override PORTDIR = ../build/r3ka

%.rel: %.s
	@# TODO: sdas should place it\'s output in the current dir
	test $(srcdir) = . || cp $< .
	-$(AS) $(ASFLAGS) $(notdir $<)
	test $(srcdir) = . || rm $(notdir $<)

include $(srcdir)/../incl.mk

Z80_FLOAT = $(COMMON_FLOAT)

Z80_INT = $(COMMON_INT)

Z80_LONG = $(COMMON_LONG) \
  _divulong.c \
  _mullong.c

Z80_LONGLONG = $(COMMON_LONGLONG) \
  _mullonglong.c \
  _divslonglong.c \
  _divulonglong.c \
  _modslonglong.c \
  _modulonglong.c

Z80_SDCC = $(COMMON_SDCC) \
  __itoa.c \
  __ltoa.c \
  _startup.c \
  div.c \
  ldiv.c \
  lldiv.c \
  sprintf.c \
  vprintf.c \
  strlen.c \
  strnlen.c \
  _strcmp.c \
  atomic_flag_clear.c

Z80SOURCES = $(addprefix ../,$(Z80_FLOAT) $(Z80_INT) $(Z80_LONG) $(Z80_LONGLONG) $(Z80_SDCC))
Z8OBJECTS = $(patsubst %.c,%.rel,$(Z80_FLOAT) $(Z80_INT) $(Z80_LONG) $(Z80_LONGLONG) $(Z80_SDCC))

OBJ = divunsigned.rel divsigned.rel divmixed.rel modunsigned.rel modsigned.rel modmixed.rel mulchar.rel \
      heap.rel memmove.rel strcpy.rel abs.rel __sdcc_call_hl.rel __sdcc_call_iy.rel crtenter.rel memcpy.rel \
      setjmp.rel atomic_flag_test_and_set.rel

LIB = r3ka.lib
AS = $(SAS)
ASFLAGS = -plosgff

CFLAGS = -I$(srcdir)/../../include -I. --std-c23

all: $(PORTDIR)/$(LIB) $(PORTDIR)/crt0.rel

$(PORTDIR)/crt0.rel: crt0.rel
	cp crt0.rel $(PORTDIR)/crt0.rel

Makefile: $(srcdir)/Makefile.in
	cd $(top_builddir); ./config.status device/lib/r3ka/Makefile

$(PORTDIR)/$(LIB): $(OBJ) $(Z8OBJECTS) Makefile
	rm -f $@
ifeq ($(LIB_TYPE), SDCCLIB)
	$(top_builddir)/bin/sdcclib -a $@ $(OBJ) $(Z8OBJECTS)
else
  ifeq ($(LIB_TYPE), AR)
	$(SDAR) -rcSD $@ $(OBJ) $(Z8OBJECTS)
  else
    ifeq ($(LIB_TYPE), RANLIB)
	$(SDAR) -rcD $@ $(OBJ) $(Z8OBJECTS)
    else
	for i in $(basename $(OBJ) $(Z8OBJECTS)); do echo $$i >>$@; done
	cp $(OBJ) $(Z8OBJECTS) $(PORTDIR)
    endif
  endif
endif

clean:
	rm -f *.rel *.sym *.lst *~ $(CLEANSPEC) *.dump* *.asm *.lib

distclean: clean
	rm -f Makefile

Makefile: $(srcdir)/Makefile.in
	cd $(top_builddir); ./config.status device/lib/r3ka/Makefile
